//This script updates the assignment workflow task to the status of"Documents Routed" after the status is updated to "Route to EPR". The documents are passed to EPR once they are in EPR we update the document status back in Accela when that is update we know the document routed sucessfully and are update the workflow letting the users know it is ready to assign the tasks

if (matches(currentUserID,"Velosimo"))
{
	showDebug = true; 
	showMessage= true;
}

var flowHistory = aa.workflow.getWorkflowHistory(capId, null).getOutput();

if(isActiveTask("Task Assignment", "Route to EPR"))
{
	var recordDocs = aa.document.getCapDocumentList(capId, lookup("VELOSIMO_SETTINGS","VELOSIMO_ACCELA_USER")).getOutput();
	var hasRouted = false;
	var hasNotRouted = false;

	for (i in recordDocs)
	{
		doc = recordDocs[i];
		if("Routed for Review".equals(doc.getDocStatus()))
		{
				hasRouted = true;
		}
	}

	logDebug("hasRouted? "+hasRouted);
	logDebug("hasNotRouted? "+hasNotRouted);

	if(hasRouted && !hasNotRouted)
	{
		updateTask("Task Assignment", "Documents Routed");
	}
}


if(isActiveTask("Task Assignment", "Route to EPR"))
{
	var recordDocs = aa.document.getCapDocumentList(capId, lookup("VELOSIMO_SETTINGS","VELOSIMO_ACCELA_USER")).getOutput();
	var hasRouted = false;
	var hasNotRouted = false;

	for (i in recordDocs)
	{
		doc = recordDocs[i];
		if("Routed for Review".equals(doc.getDocStatus()))
		{
				hasRouted = true;
		}
	}

	logDebug("hasRouted? "+hasRouted);
	logDebug("hasNotRouted? "+hasNotRouted);

	if(hasRouted && !hasNotRouted)
	{
		updateTask("Assignment", "Documents Routed");
	}
}